package com.quectel.multicamera;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.view.ViewGroup;

import androidx.fragment.app.Fragment;

import com.quectel.qcarapi.stream.QCarCamera;
import com.quectel.qcarapi.util.QCarLog;


public class SurfaceViewFragment extends Fragment {
    private static final String TAG = "PreviewFragment";
    private SurfaceView preview;
    private SurfaceHolder surfaceHolder;
    private int mChannel;
    private QCarCamera qCarCamera;
    private int mIsPreview;
    private int preWidth;
    private int preHeight;


    public SurfaceViewFragment() {

    }

    @SuppressLint("ValidFragment")
    public SurfaceViewFragment(QCarCamera qCarCamera, int channel, int ispreview) {
        this.qCarCamera = qCarCamera;
        this.mChannel = channel;
        this.mIsPreview = ispreview;
    }

    public void setPreviewSize(int width, int height) {
        preWidth = width;
        preHeight = height;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        if (qCarCamera != null) {
            QCarLog.i(QCarLog.LOG_MODULE_APP, TAG, "get csi num "+qCarCamera.getCsiNum()+" channel "+mChannel+" IsPreview "+mIsPreview);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_surface_view, container, false);

        preview = (SurfaceView) rootView.findViewById(R.id.preview);
        surfaceHolder = preview.getHolder();

        surfaceHolder.addCallback(new SurfaceHolder.Callback() {
            @Override
            public void surfaceCreated(SurfaceHolder holder) {
                QCarLog.i(QCarLog.LOG_MODULE_APP, TAG, "onCreateView set csi num "+qCarCamera.getCsiNum()+" channel "+mChannel+" IsPreview "+mIsPreview);

                if(mIsPreview == 1) {
                    qCarCamera.startPreview( mChannel,holder.getSurface(), preWidth, preHeight,QCarCamera.YUV420_NV21);
                }
            }

            @Override
            public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

            }

            @Override
            public void surfaceDestroyed(SurfaceHolder holder) {
                QCarLog.i(QCarLog.LOG_MODULE_APP, TAG, "surfaceDestroyed: mCsiphyNum = " + qCarCamera.getCsiNum() + ", mChannel = " + mChannel);
                if(mIsPreview == 1) {
                    qCarCamera.stopPreview( mChannel);
                    try{
                        Thread.sleep(100);
                    }catch(Exception e){

                    }
                }
            }
        });

        return rootView;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    @Override
    public void onStop() {
        QCarLog.i(QCarLog.LOG_MODULE_APP, TAG, "onStop");
        super.onStop();
    }

    @Override
    public void onDestroy() {
        QCarLog.i(QCarLog.LOG_MODULE_APP, TAG, "onDestroy");
        super.onDestroy();
    }
}
